package com.fmy.mcsecurity.config.builders;

import com.fmy.mcsecurity.config.filter.JITokenContextFilter;
import com.fmy.mcsecurity.config.filter.RefreshTokenFilter;
import com.fmy.mcsecurity.config.filter.UsernamePassParamsFilter;
import com.fmy.mcsecurity.integration.ClientSupportIntegration;
import com.fmy.mcsecurity.login.hander.LoginHandler;
import com.fmy.mcsecurity.token.TokenGenerate;
import org.springframework.context.ApplicationContext;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.Assert;

/**
 * @author Mr.fmy
 * @version 1.0.0
 * @ClassName HttpJISecurity
 * @Description TODO 自定义配置类
 * @createTime 2019/10/23 12:52
 */
public class HttpMcSecurity {
    private final UsernamePassParamsFilter usernamePassParamsFilter;
    private final JITokenContextFilter jiTokenContextFilter;
    private final RefreshTokenFilter refreshTokenFilter;
    private final TokenGenerate tokenGenerate;

    public HttpMcSecurity(UsernamePassParamsFilter usernamePassParamsFilter, JITokenContextFilter
            jiTokenContextFilter, RefreshTokenFilter refreshTokenFilter, ApplicationContext context) {
        this.usernamePassParamsFilter = usernamePassParamsFilter;
        this.jiTokenContextFilter = jiTokenContextFilter;
        this.refreshTokenFilter = refreshTokenFilter;
        this.tokenGenerate = context.getBean(TokenGenerate.class);
        this.usernamePassParamsFilter.setLoginHandler(context.getBean(LoginHandler.class));
        this.usernamePassParamsFilter.setClientSupportIntegration(context.getBean(ClientSupportIntegration.class));
    }

    public HttpMcSecurity setUserPassParamsMatcher(String path) {
        usernamePassParamsFilter.setMatcher(new AntPathRequestMatcher(path, "POST"));
        return this;
    }

    public HttpMcSecurity setUserPassParamsName(String... paramNames) {
        usernamePassParamsFilter.setParamNames(paramNames);
        return this;
    }

    public HttpMcSecurity setBearer(String bearer) {
        jiTokenContextFilter.setBearer(bearer);
        return this;
    }

    public HttpMcSecurity setTokenKey(String token) {
        jiTokenContextFilter.setTokenKey(token);
        return this;
    }

    public HttpMcSecurity setRefresh(String refresh) {
        refreshTokenFilter.setRefresh(refresh);
        return this;
    }

    public HttpMcSecurity setRefreshTokenMatcher(String path) {
        refreshTokenFilter.setMatcher(new AntPathRequestMatcher(path, "POST"));
        return this;
    }

    public HttpMcSecurity setTokenContextUrl(String... urls) {
        return setTokenContextUrl(true, urls);
    }

    public HttpMcSecurity setTokenContextUrl(boolean matcher, String... urls) {
        Assert.notEmpty(urls, "请传入拦截 url");
        RequestMatcher[] pathRequestMatchers = new RequestMatcher[urls.length];
        for (int i = 0; i < urls.length; i++) {
            pathRequestMatchers[i] = new AntPathRequestMatcher(urls[i]);
        }
        jiTokenContextFilter.setMatcher(new OrRequestMatcher(pathRequestMatchers));
        jiTokenContextFilter.setNotMatcher(matcher);
        return this;
    }


    public <T extends TokenGenerate> T getTokenGenerate(Class<T> c) {
        if (tokenGenerate.getClass().isAssignableFrom(c)) {
            return (T) tokenGenerate;
        }
        throw new IllegalArgumentException(tokenGenerate.getClass().toString() + " not is " + c.toString());
    }

    public HttpMcSecurity setLoginHandler(LoginHandler loginHandler) {
        this.usernamePassParamsFilter.setLoginHandler(loginHandler);
        return this;
    }
}
